<?php

function omeka_addons_manage_form($type)
{
    if($type == 'plugin') {
        $page = 'omeka-plugins';
    } elseif($type == 'theme') {
        $page = 'omeka-themes';
    }
    
    $id = $_GET['id'];
    $version = $_REQUEST['tag_directory'];
    $action = $_REQUEST['action'];
    
    if(!empty($id)) {
        $addon = omeka_addons_get_addon_by_id($id);
    }
    
    if(!empty($action)) {
        if ($action == 'create_addon') {
            omeka_addons_create_addon();
        } elseif ($action == 'delete_addon' || $action == 'save_addon') {
            if(!empty($id)) {
                if($action == 'save_addon') {
                    omeka_addons_edit_addon($id);
                } elseif($action == 'delete_addon') {
                    omeka_addons_delete_addon($id);
                    echo '<div class="updated"><p>'.ucwords($type).' deleted successfully.</p></div>';        
                }
            } else {
                echo '<div class="error"><p>No ID given.</p></div>';
            }
        } elseif($action == 'show_version') {
             $versionData = omeka_addons_get_ini_data($id, $version);
        } elseif($action == 'create_version') {
            omeka_addons_create_version();
        } elseif($action == 'delete_version') {
            omeka_addons_delete_version($id);
        }
    }
?>

<div class="wrap">
    <h2><?php echo ucwords($type); ?> | Omeka Addons</h2>
    
    <?php if($action == 'get_version'): ?>
        
    <h3>Get a Version for the <em><?php echo $addon->directory_name; ?></em> <?php echo $addon->type; ?></h3>
    <?php omeka_addons_get_version_form($action, $id, $type); ?>
    
    <?php elseif($action == 'show_version'): ?>
        
        <?php

        if(isset($_REQUEST['get_version'])) {
            $versionData = omeka_addons_get_ini_data($id, $version);
        }
        ?>
        <h3>The .ini file for version <?php echo $version; ?> of the <em><?php echo $addon->directory_name; ?></em> <?php echo $addon->type; ?> is:</h3>
        
        <?php omeka_addons_add_version_form($versionData, $type); ?>
        
    <?php elseif($action == 'edit_addon'): ?>

        <?php echo omeka_addons_edit_addon_form($id); ?>

    <?php else: ?>
        <?php 
        omeka_addons_form($type);
        $addons = omeka_addons_get_addons($type, array(), false);
        if($addons): 
        
        ?>

        <table class="widefat fixed" cellspacing="0">
            <thead>
        	<tr>
            	<th scope="col" class="manage-column" style=""><?php echo ucwords($type); ?> Name</th>
            	<th scope="col" class="manage-column" style=""><?php echo ucwords($type); ?> Description</th>
            	
            	<th scope="col" class="manage-column" style="width:20%;">Released Versions</th>
        	</tr>
        	</thead>
        	<tfoot>
        	<tr>
            	<th scope="col" class="manage-column" style=""><?php echo ucwords($type); ?> Name</th>
            	<th scope="col" class="manage-column" style=""><?php echo ucwords($type); ?> Description</th>
            	
            	<th scope="col" class="manage-column" style="width:20%;">Released Versions</th>
        	</tr>
        	</tfoot>
        	<tbody>
        <?php 
        foreach($addons as $addon): 
        $addonName = !empty($addon->addon_name) ? $addon->addon_name : $addon->directory_name;

        ?>
            <tr valign="center">
                <th scope="row" class="column-name">
                    <h3><?php echo $addonName; ?></h3>
                    

                    <div class="row-actions">
                        <a href="<?php echo omeka_addons_get_management_url($page, 'edit_addon', $addon->id); ?>">Edit Info</a> | <a class='submitdelete' href="<?php echo omeka_addons_get_management_url($page, 'delete_addon', $addon->id);?>" onclick="if ( confirm('You are about to delete the <?php echo $addon->directory_name; ?> plugin and all the versions currently saved on the site. Click Cancel to stop, OK to delete.') ) { return true;}return false;">Delete</a> | <a href="<?php echo omeka_addons_get_management_url($page, 'get_version', $addon->id);?>" title="Add a new version for the <?php echo $addon->directory_name; ?> plugin">Add a new Version</a></span>
                    </div>
                </th>
                <td>
                    
                    <br />
                    <?php 
                    $versions = omeka_addons_get_versions($addon->id);
                    
                    $addonDescription = $addon->addon_description;
                    if($versions): 
                    if(empty($addonDescription)) {
                          $addonDescription = $versions[0]->description;
                    }
                    ?>
                    <?php endif; ?>
                    
                    <p><?php echo $addonDescription; ?></p>
                </td>
                <td>
                    <br />
                    
                    <?php if($versions): ?>
                        <ul class="version-list">
            	    <?php foreach($versions as $version): ?>
            	        <li class="off"><?php echo $version->tag_directory; ?> <span class="row-actions">| <a href="<?php echo omeka_addons_get_management_url($page, 'delete_version', $version->id); ?>" onclick="if ( confirm('You are about to delete version <?php echo $version->version; ?> for the <?php echo $addon->directory_name; ?> plugin. This will also delete the zip file from the server. Cancel to stop, OK to delete.') ) { return true;}return false;">Delete this version</a><span></li>
            	    <?php endforeach; ?>
            	        </ul>
            	    <?php endif; ?>
            	    <p></p>
                </td>
            </tr>
        <?php endforeach; ?>
        </tbody>
    </table>
    <?php else: ?>
        <p>You have no <?php echo $type; ?>s in your database. Use that sweet select menu to add one.</p>
        <?php endif; ?>
    <?php endif; ?>
    <div class="clear"></div>
    
    </div>
<?php    
}

function omeka_addons_edit_addon_form($addonId)
{
    if(!$addonId) return;
    $addon = omeka_addons_get_addon_by_id($addonId);
    
    // print_r($addon);
    $addonName = !empty($addon->addon_name) ? $addon->addon_name : $addon->directory_name;
    $addonDescription = !empty($addon->addon_description) ? $addon->addon_description : '';    
    
?>
<form method="post" action="">
    <h3>Edit <?php echo $addonName; ?> Information</h3>
    
    <table class="form-table">
        <tr>
            <th scope="row"><label for="addon_name">Addon Name</label></th>
            <td><input type="text" name="addon_name" value="<?php echo $addonName; ?>"/></td>
        </tr>
        <tr>
            <th scope="row"><label for="addon_description">Addon Description</label></th>
            <td><textarea name="addon_description" style="width:70%;" rows="20"><?php echo $addonDescription; ?></textarea></td>
        </tr>
        <tr>
            <th scope="row"><label for="featured">Featured?</label></th>
            <td><input type="checkbox" name="featured" value="1" <?php if($addon->featured) echo ' checked="checked"'; ?>/></td>
            
        </tr>
    </table>
    <input type="hidden" name="action" value="save_addon" id="action" />
    
    <p class="submit">
		<input type="submit" name="update_addon" class="button-primary" value="Save Changes" />
	</p>
</form>
<?php 
}


function omeka_addons_form($type)
{
    if(!$type && ($type != 'plugin' || $type != 'theme')) return;
?>
<form method="post" style="float:right;" action="">
    <label for="directory_name">Add a new <?php echo $type; ?></label>
<select name="directory_name" id="directory_name">
    <option>Choose a <?php echo ucwords($type); ?></option>
<?php 
$allAddons = omeka_addons_get_repository_directories($type);
$savedAddons= omeka_addons_get_addons($type);


$currentAddons = array();
foreach($savedAddons as $savedAddon) {
    $currentAddons[] = $savedAddon->directory_name;
}


foreach($allAddons as $addon):
if(in_array($addon, $currentAddons)) continue;
?>
<option value="<?php echo $addon; ?>"><?php echo $addon; ?></option>
<?php endforeach; ?>   
</select>


                <input type="hidden" name="type" value="<?php echo $type; ?>" />
                <input type="hidden" name="action" value="create_addon" />
    
		<input type="submit" name="save" class="button-primary" value="Continue" />
		<br class="clear" />
		<p></p>
</form>

<?php
}

function omeka_addons_addon_form($addonId)
{
    if(!$addonId) return;
?>
<form method="post" style="float:right;" action="">
    <label for="addon_name">Add-On Name</label>
    <input type="text" name="addon_name" />
    
    <label for="addon_description">Description</label>
    <textarea name="addon_description">
        
    </textarea>
    
		<input type="submit" name="save" class="button-primary" value="Continue" />
		<br class="clear" />
		<p></p>
</form>

<?php
}


function omeka_addons_get_version_form($mode='show_version', $addonId=false, $type=null)
{
    if(!$type && ($type != 'plugin' || $type != 'theme')) return;
    
    if($type == 'plugin') {
        $page = 'omeka-plugins';
    } elseif($type == 'theme') {
        $page = 'omeka-themes';
    }
    
    $addon = omeka_addons_get_addon_by_id($addonId);
    
?> 
    <form method="post" action="<?php echo omeka_addons_get_management_url($page, 'show_version', $addonId); ?>">
        <input type="hidden" name="addon_id" value="<?php echo $addonId; ?>" />
        <input type="hidden" name="action" value="show_version" />
        
        <table class="form-table">
            <tr valign="top">
                <th scope="row"><label for="directory_name">Available Tags</label></th>
                <td>
                    <?php 
                    $tags = omeka_addons_get_repository_directories($type, $addon->directory_name); 
                    
                    $versions = omeka_addons_get_versions($addonId);
                    
                    $currentVersions = array();
                    foreach($versions as $version) {
                        $currentVersions[] = $version->tag_directory;
                    }
                    ?>
                    <?php if($tags): ?>
                    <select name="tag_directory" id="tag_directory">
                        <option>Choose</option>
                        <?php foreach($tags as $tag): ?>
                            <?php if(in_array($tag,$currentVersions)) continue; ?>
                            <option value="<?php echo $tag; ?>"><?php echo $tag; ?></option>
                        <?php endforeach;?>
                    </select>
                    <p class="description">The directory for the <?php echo $type ?>'s tag repository.</p>
                    <?php else: ?>
                        <p>There are no tags for this <?php echo $type; ?>!</p>
                    <?php endif; ?>
                </td>
            </tr>
        </table>
        <p class="submit">
    		<input type="submit" name="show_version" class="button-primary" value="Get INI Information" />
    	</p>
    </form>
    
<?php
}

function omeka_addons_add_version_form($versionData, $type, $mode="create_version")
{
   $tagDirectory = !empty($_REQUEST['tag_directory']) ? $_REQUEST['tag_directory'] : '';
   $addonId = !empty($_REQUEST['id']) ? $_REQUEST['id'] : '';

   if($type == 'plugin') {
       $page = 'omeka-plugins';
       $versionData = $versionData['info'];
   } elseif($type == 'theme') {
       $page = 'omeka-themes';
       $versionData = $versionData['theme'];
   }
   if($versionData):
?>
    <form method="post" action="<?php echo omeka_addons_get_management_url($page); ?>">
        <input type="hidden" name="addon_id" value="<?php echo $addonId; ?>" />
        <input type="hidden" name="action" value="<?php echo $mode; ?>" />
        <input type="hidden" name="tag_directory" value=<?php echo $tagDirectory; ?> />
        
        <?php foreach($versionData as $name => $value): ?>
        <input type="hidden" name="<?php echo $name; ?>" value="<?php echo htmlentities($value); ?>" />
        <?php endforeach; ?>
        <table cellpadding="10px">
        <?php foreach($versionData as $name => $value): ?>
            <tr valign="top" align="left">
                <th scope="row"><?php echo $name; ?></th>
                <td><?php echo htmlentities($value); ?></td>
            </tr>
        <?php endforeach; ?>
        </table>
        <p class="submit">
    		<input type="submit" name="save_version" class="button-primary" value="Create Version" />
    	</p>
    </form>
    <?php else: ?>
        <p>No version data provided!</p>
    <?php endif; ?>
<?php
}